// var DB = require("../lib/db_pg").DB;
var DB = require("../lib/db_mysql").DB;
var config = require('../lib/config');
var resErr = require('../lib/util.js').resErr;
var md5 = require('../lib/util.js').md5;
var async = require('async');
var http_request = require('../lib/util.js').http_request;

exports.param_list = function(req,res){
    var param = req.param('param');
    var sql_text = "SELECT u.* FROM t_sys_param u WHERE 1=1";
    var tmp_params = [];
    var i=1;
    if(param) {
        sql_text += " and upper(param_cd) like upper('" + param + "')";
    }
    
    var limit = parseInt(req.param("limit") || req.param("pageSize") ||'20');
    var page  = parseInt(req.param("page") || req.param("pageNum") || '1');  
    if(req.param("sort")){
        sql_text += " order by u." + req.param("sort");
        if(req.param("dir")){
            sql_text += " " + req.param("dir");
        }
    }
    
    var db = new DB();
    db.queryPage(sql_text,tmp_params,page,limit,function(err,result){
        if(err) return resErr(res, err);
            return res.json(result);
    });
};

exports.param_add = function(req, res){
    var param_cd = req.param('param_cd');
    if(!param_cd) return res.json({success:false,msg:'参数代码不能为空！'});
    var param_desc = req.param('param_desc');
    if(!param_desc) return res.json({success:false,msg:'参数描述不能为空！'});
    var param_value = req.param('param_value');
    if(!param_value) return res.json({success:false,msg:'参数值不能为空！'});
    var db = new DB();
    
    async.waterfall([
        function(cb){
            var tmp_params = [];
            var sql_text = 'select count(1) from t_sys_param where param_cd = ?;';
            tmp_params.push(param_cd);
            db.query(sql_text,tmp_params,function(err,datas){
                if(err) return cb(err);
                if(datas.length > 0 && datas[0].counter > 0){
                  return cb('参数代码已经存在！');
                }
                return cb(null);
            });
        }
    ], function(err){ // callback
        if(err) return resErr(res, err);
        var tmp_params = [];
        var sql_text = 'insert into t_sys_param(param_cd, param_desc, param_value) values(?, ?, ?);';
        tmp_params.push(param_cd);
        tmp_params.push(param_desc);
        tmp_params.push(param_value);
        db.query(sql_text,tmp_params,function(err,datas){
            console.log(datas);
            if (err){
                return resErr(res,err);
            }      
            return res.json({success:true,msg:'参数添加成功！'});
        });
    });
};

exports.param_del = function(req, res){
    var param_cd = req.param('param_cd');
    if(!param_cd) return res.json({success: false, msg: '参数代码不能为空'});
    
    var db = new DB();
    var sql_text = 'delete from t_sys_param where param_cd = ?;';
    
    var tmp_params = [];
    tmp_params.push(param_cd);
    
    db.query(sql_text, tmp_params, function(err, datas){
        console.log(datas);
        if(err){
            return resErr(res, err);
        }
        return res.json({success: true, msg: '参数删除成功'});
    });
};

exports.param_upd = function(req, res){
    var param_cd = req.param('param_cd');
    if(!param_cd) return res.json({success: false, msg: '参数代码不能为空'});
    var param_desc = req.param('param_desc');
    if(!param_desc) return res.json({success:false,msg:'参数描述不能为空！'});
    var param_value = req.param('param_value');
    if(!param_value) return res.json({success:false,msg:'参数值不能为空！'});
    
    var db = new DB();
    var sql_text = 'update t_sys_param set param_desc = ?, param_value = ? where param_cd = ?;';
    
    var tmp_params = [];
    tmp_params.push(param_desc);
    tmp_params.push(param_value);
    tmp_params.push(param_cd);
    
    db.query(sql_text, tmp_params, function(err, datas){
        console.log(datas);
        if(err){
            return resErr(res, err);
        }
        return res.json({success: true, msg: '参数修改成功'});
    });
};